﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;

namespace l1cs.server.model
{
    public class L1Buddy
    {
        private int _charId;

        private SortedList<int, String> _buddys = new SortedList<int, String>();

        public L1Buddy( int charId )
        {
            _charId = charId;
        }

        public int getCharId()
        {
            return _charId;
        }

        public bool add( int objId, String name )
        {
            if ( _buddys.ContainsKey( objId ) )
            {
                return false;
            }
            _buddys[objId] = name;
            return true;
        }

        public bool remove( int objId )
        {
            bool result = _buddys.Remove( objId );
            return result;
        }

        public bool remove( String name )
        {
            int id = 0;
            //foreach (Map.Entry<Integer, String> buddy in _buddys.entrySet()) {
            foreach ( int key in _buddys.Keys )
            {
                if ( string.Compare( name, _buddys[key], true ) == 0 )
                {
                    id = key;
                    break;
                }
            }
            if ( id == 0 )
            {
                return false;
            }
            _buddys.Remove( id );
            return true;
        }

        public String getOnlineBuddyListString()
        {
            String result = "";
            foreach ( L1PcInstance pc in L1World.getInstance().getAllPlayers() )
            {
                if ( _buddys.ContainsKey( pc.getId() ) )
                {
                    result += pc.getName() + " ";
                }
            }
            return result;
        }

        public String getBuddyListString()
        {
            String result = "";
            foreach ( String name in _buddys.Values )
            {
                result += name + " ";
            }
            return result;
        }

        public bool containsId( int objId )
        {
            return _buddys.ContainsKey( objId );
        }

        public bool containsName( String name )
        {
            foreach ( String buddyName in _buddys.Values )
            {
                if ( string.Compare( name, buddyName,true ) ==0)
                {
                    return true;
                }
            }
            return false;
        }

        public int size()
        {
            return _buddys.Count;
        }
    }

}
